import {createRouter, createWebHashHistory, createWebHistory} from "vue-router";
import store from '../store'
import Main from '../pages/main.vue'
import app from '../configs/app'

const modules = import.meta.glob('../pages/**/*.vue')

const onFamilyMemberEditEnter = (to, from) => {
    // console.log(to, from)
}

/**
 * @param path Path of component
 * @param name Use by keep-alive
 * @returns {(function(*=): *)|*|(function(*): *)}
 */
// const myImport = (path, name) => {
//     const proxy = import(path)
//     proxy.then((component) => {
//         component.default.name = name
//     })
//     return proxy
// }

/**
 * @param path Path of component
 * @param name Use by keep-alive
 * @returns {(function(*=): *)|*|(function(*): *)}
 */
const myImport = (path, name) => {
    return modules[path]().then((component) => {
        component.default.name = name
        return component
    })
}

const routes = [
    {
        path: '/',
        name: "home",
        component: () => myImport('../pages/home.vue', 'home'),
        meta: { title: "郑州植得口腔医院"},
    }, {
        path: "/login",
        name: "wx-auth",
        component: () => myImport('../pages/login.vue', 'wx-auth'),
        meta: { title: "授权登录"},
    }, {
        path: "/right_got",
        name: "right_got",
        component: () => myImport('../pages/right_got.vue', 'right_got'),
        meta: { title: "权益解锁进度", login: true},
    }, {
        path: "/poster",
        name: "poster",
        component: () => myImport('../pages/poster.vue', 'poster'),
        meta: { title: "分享海报", login: true},
    }, {
        path: "/write_off_member",
        name: "write_off_member",
        component: () => myImport('../pages/write_off_member.vue', 'write_off_member'),
        meta: { title: "核销员", login: true},
    }, {
        path: "/right_write_off",
        name: "right_write_off",
        component: () => myImport('../pages/right_write_off.vue', 'right_write_off'),
        meta: { title: "权益核销", login: true},
    }, {
        path: "/share",
        name: "share",
        component: () => myImport('../pages/share.vue', 'share'),
        meta: { title: "邀请明细", login: true},
    }, {
        path: "/assistance",
        name: "assistance",
        component: () => myImport('../pages/assistance.vue', 'share'),
        meta: { title: "邀请明细", login: true},
    }, {
        path: "/rank",
        name: "rank",
        component: () => myImport('../pages/rank.vue', 'rank'),
        meta: { title: "我的收益", login: true},
    }, {
        path: "/cash",
        name: "cash",
        component: () => myImport('../pages/cash.vue', 'cash'),
        meta: { title: "我的收益", login: true},
    }, {
        path: "/withdraw",
        name: "withdraw",
        component: () => myImport('../pages/withdraw.vue', 'withdraw'),
        meta: { title: "申请提现", login: true},
    }, {
        path: "/user_info",
        name: "user_info",
        component: () => myImport('../pages/user_info.vue', 'user_info'),
        meta: { title: "信息完善", login: true},
    }, {
        path: "/order",
        name: "order",
        component: () => myImport('../pages/order.vue', 'order'),
        meta: { title: "订单", login: true},
    }, {
        path: "/test_order",
        name: "test_order",
        component: () => myImport('../pages/test_order.vue', 'test_order'),
        meta: { title: "收银", login: true},
    }, {
        path: "/coupons/detail",
        name: "coupon_detail",
        component: () => myImport('../pages/coupons/detail.vue', 'coupon_detail'),
        meta: { title: "卡券详细", login: true},
    }, {
        path: "/coupons/qrcode",
        name: "coupon_qrcode",
        component: () => myImport('../pages/coupons/qrcode.vue', 'coupon_qrcode'),
        meta: { title: "卡券核销", login: true},
    }
]

const router = createRouter({
    history: app.history == 'history' ? createWebHistory() : createWebHashHistory(),
    routes,
})

const routerQueue = []

router.beforeEach(async (to, from, next) => {

    if (routerQueue[routerQueue.length - 1] == to.path + '=>' + from.path) {
        routerQueue.pop()
    } else {
        routerQueue.push(from.path + '=>' + to.path)
    }

    console.log(routerQueue)

    if (to.meta.keepAlive) {
        store.commit('keepAlive', to.name)
    }

    let user = await store.dispatch("user/getUser")

    if (to.meta['login']) {
        if (!user) { // Not login
            localStorage.setItem('backPage', to.fullPath)
            next('/login')
        } else {
            next()
        }
    } else {
        next()
    }

})

export function resetRouter () {
    const newRouter = createRouter({
        history: createWebHashHistory(),
        routes,
    })
    router.matcher = newRouter.matcher // the relevant part
}

export default router
